package com.sandking.db.jedis;

import com.sandking.db.dao.YhpvrzDao;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import com.sandking.db.bean.Yhpvrz;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class YhpvrzJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhpvrz getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhpvrz yhpvrz = null;
		try{
			String key = "Yhpvrz_Object";
			String field = SK_Plus.b("id:",id).e();
			yhpvrz = Yhpvrz.createForJson(jedis.hget(key,field));
			if(yhpvrz == null){
				yhpvrz = YhpvrzDao.getById(id);
				if(yhpvrz!=null){
					loadCache(yhpvrz);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhpvrz;
	}
	

	public static void loadCache(Yhpvrz yhpvrz){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhpvrz,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhpvrz yhpvrz,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhpvrz_Object";
		String field = SK_Plus.b("id:",yhpvrz.getId()).e();
		String data = yhpvrz.toJson();
		p.hset(key,field,data);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhpvrz> yhpvrzs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhpvrz yhpvrz : yhpvrzs){
				loadCache(yhpvrz,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhpvrz yhpvrz){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhpvrz,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhpvrz yhpvrz,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhpvrz_Object";
		String field = SK_Plus.b("id:",yhpvrz.getId()).e();
		p.hdel(key,field);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhpvrz> yhpvrzs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhpvrz yhpvrz : yhpvrzs){
				clearCache(yhpvrz,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhpvrz insert(Yhpvrz yhpvrz){
		yhpvrz = YhpvrzDao.insert(yhpvrz);
		if(yhpvrz!=null){
			loadCache(yhpvrz);
		}
    	return yhpvrz;
    }
    
    public static Yhpvrz update(Yhpvrz yhpvrz){
    	yhpvrz = YhpvrzDao.update(yhpvrz);
    	if(yhpvrz!=null){
			loadCache(yhpvrz);
		}
    	return yhpvrz;
    }
    
    public static boolean delete(Yhpvrz yhpvrz){
    	boolean bool = YhpvrzDao.delete(yhpvrz);
    	if(bool){
    		clearCache(yhpvrz);
    	}
    	return bool;
    }
}